package woaini.wind.bot.code.impl.weChatFerry.adapter;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONPath;
import woaini.wind.bot.core.adapter.HttpAdapter;
import woaini.wind.bot.core.bot.Bot;
import woaini.wind.bot.core.event.action.ActionRequest;
import woaini.wind.bot.core.event.action.ActionResponse;
import woaini.wind.bot.core.event.base.Event;
import woaini.wind.bot.core.event.base.Self;
import woaini.wind.bot.core.event.message.MessageEvent;
import woaini.wind.bot.core.event.message.impl.GroupMessageEvent;
import woaini.wind.bot.core.event.message.impl.PrivateMessageEvent;
import woaini.wind.bot.core.event.segment.Messages;
import woaini.wind.bot.core.utils.XmlTool;

public class WeChatFerryBotHttpAdapter extends HttpAdapter {

  public WeChatFerryBotHttpAdapter(Bot bot) {
    super(bot);
  }

  @Override
  public boolean connect() {
    return super.connect();
  }

  @Override
  public Event decode(String message) {
    Event event = null;
    JSONObject source = JSONObject.parseObject(message);
    //获取原始事件类型 目前知道 1是消息
    Integer postType = source.getInteger("type");
    if (ObjectUtil.isEmpty(postType)){
      return event;
    }
    event = new Event();
    //消息id
    String messageId = String.valueOf(source.getLong("id"));
    event.setId(messageId);
    //消息来源
    Self self = new Self();
    self.setPlatform("wx");
    //没地方取 从配置文件吧
    String selfId = this.bot.getConfig().getSelfId();
    self.setUserId(selfId);
    event.setSelf(self);

    //时间
    Long time = source.getLong("ts");
    event.setTime(time);
    event.setMessageId(messageId);
    //      "0": "朋友圈消息",
    //      "1": "文字",
    //      "3": "图片",
    //      "34": "语音",
    //      "37": "好友确认",
    //      "40": "POSSIBLEFRIEND_MSG",
    //      "42": "名片",
    //      "43": "视频",
    //      "47": "石头剪刀布 | 表情图片",
    //      "48": "位置",
    //      "49": "共享实时位置、文件、转账、链接",
    //      "50": "VOIPMSG",
    //      "51": "微信初始化",
    //      "52": "VOIPNOTIFY",
    //      "53": "VOIPINVITE",
    //      "62": "小视频",
    //      "66": "微信红包",
    //      "9999": "SYSNOTICE",
    //      "10000": "红包、系统消息",
    //      "10002": "撤回消息",
    //      "1048625": "搜狗表情",
    //      "16777265": "链接",
    //      "436207665": "微信红包",
    //      "536936497": "红包封面",
    //      "754974769": "视频号视频",
    //      "771751985": "视频号名片",
    //      "822083633": "引用消息",
    //      "922746929": "拍一拍",
    //      "973078577": "视频号直播",
    //      "974127153": "商品链接",
    //      "975175729": "视频号直播",
    //      "1040187441": "音乐链接",
    //      "1090519089": "文件"
    // 消息内容
    String rawMessage = source.getString("content");
    event.setAltMessage(rawMessage);
    switch (postType){
      case 1 -> {
        event = this.decodeMessageEvent(source, event);
      }
      default -> {}
    }
    return event;
  }
  public MessageEvent decodeMessageEvent(JSONObject source, Event baseEvent) {

    //发送人id
    String sendId = source.getString("sender");
    Messages messages = this.decodeMessages(source);
    baseEvent.setMessage(messages);
    //获取是否群聊消息
    boolean isGroup = source.getBoolean("is_group");
    if (isGroup){
      GroupMessageEvent groupMessageEvent = new GroupMessageEvent();
      BeanUtil.copyProperties(baseEvent,groupMessageEvent);
      groupMessageEvent.setUserId(sendId);
      //群组id
      String groupId = source.getString("roomid");
      groupMessageEvent.setGroupId(groupId);
      return groupMessageEvent;
    }else {
      //解析私聊消息
      PrivateMessageEvent privateMessageEvent = new PrivateMessageEvent();
      BeanUtil.copyProperties(baseEvent,privateMessageEvent);
      privateMessageEvent.setUserId(sendId);
      return privateMessageEvent;
    }

  }

  public Messages decodeMessages(JSONObject source) {

    String rawMessage = source.getString("content");

    Messages messages = Messages.builder();
    Integer postType = source.getInteger("type");
    switch (postType){
      case 1 -> {
        //文本消息
        messages.text(rawMessage);
      }
      case null, default -> {}
    }

    //分析xml信息
    String xml = source.getString("xml");
    if (StrUtil.isEmpty(xml)){
      return messages;
    }
    xml = xml.replaceAll(" ", "").replace("\n","");

    String xmlToJson = XmlTool.convertXmlToJson(xml);
    if (StrUtil.isEmpty(xmlToJson)){
      return messages;
    }
    String atuserlist = (String) JSONPath.eval(xmlToJson, "$.atuserlist");
    if(StrUtil.isNotEmpty(atuserlist)){
      for (String atId : StrUtil.split(atuserlist, ",")) {
        if (StrUtil.isEmpty(atId)){
          continue;
        }
        if (atId.equals("notify@all")){
          messages.atAll();
        }else {
          messages.at(atId);
        }
      }
    }

    return messages;
  }

  @Override
  public String encode(ActionRequest action) {
    return "";
  }

  @Override
  public ActionResponse action(ActionRequest actionRequest) {
    return null;
  }
}
